<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/6
 * Time: 12:03
 */

namespace app\api\service;


use think\Exception;

class WxMessage
{
    private $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=%s';

    protected $touser;
    protected $template_id;
    protected $form_id;
    protected $page;
    protected $data;

    function __construct(){
        $AccessToken = new AccessToken();
        $ac = $AccessToken->get();

        $this->url = sprintf($this->url, $ac);
    }

    protected function sendToUser(){

        $params = [
            'touser' => $this->touser,
            'template_id' => $this->template_id,
            'page' => $this->page,
            'form_id' => $this->form_id,
            'data' => $this->data,
        ];

        $result = curl_post($this->url, $params);

        $result = json_decode($result, true);
        if ($result['errcode'] == 0) {
            return true;
        } else {
            return false;
            // throw new Exception('模板消息发送失败,  ' . $result['errmsg']);
        }
    }
}